package com.tedu.weibo.controller;

import com.tedu.weibo.common.result.JsonResult;
import com.tedu.weibo.common.result.StatusCode;
import com.tedu.weibo.mapper.UserMapper;
import com.tedu.weibo.pojo.dto.UserLoginDTO;
import com.tedu.weibo.pojo.dto.UserRegDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Api(tags = "用户模块")
@RestController
@RequestMapping("/v1/users")
public class UserController {
    @Autowired
    private UserMapper userMapper;


    @ApiOperation("注册用户")
    @PostMapping("/reg")
    public JsonResult reg(@RequestBody UserRegDTO userRegDTO){
        System.out.println(userRegDTO);

        String username=userRegDTO.getUsername();
        User findUser=userMapper.selectByUserName(username);
        if (findUser !=null){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }

        User user=new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());
        userMapper.insert(user);
        return JsonResult.success();
      //  return new JsonResult(StatusCode.OPERATION_SUCCESS/*2001,"操作成功"*/);

    }


    @ApiOperation("用户登录")
    @PostMapping("/login")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO ,@ApiIgnore HttpSession session){
        System.out.println(userLoginDTO);

        User user=userMapper.selectByUserName(userLoginDTO.getUsername());
        if (user!=null){
            if (user.getPassword().equals(userLoginDTO.getPassword())){
                session.setAttribute("user",user);
                return JsonResult.success();
               // return new JsonResult(StatusCode.OPERATION_SUCCESS/*2001,"操作成功"*/);
            }else {
                return new JsonResult(StatusCode.PASSWORD_ERROR/*1001,"密码错误"*/);
            }
        }else {
            return JsonResult.success();
           // return new JsonResult(StatusCode.USERNAME_ERROR/*1000,"用户名错误"*/);
        }
    }

    @ApiOperation("查询用户登录状态")
    @GetMapping("/currentUser")
    public JsonResult currentUser(@ApiIgnore HttpSession session){
       User user=(User)session.getAttribute("user");
        if (user==null){
            return JsonResult.success();
           // return new JsonResult(StatusCode.OPERATION_SUCCESS);
        }else {
            UserVO userVO=new UserVO();
            BeanUtils.copyProperties(user,userVO);
            return JsonResult.success(userVO);
          //  return new JsonResult(StatusCode.OPERATION_SUCCESS,userVO);
        }
    }


    @ApiOperation("用户登出")
    @GetMapping("/logout")
    public void loout(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
    }


}
